/**
 * 
 */
package com.gr.staffpm.pages.projects;

import java.util.List;

import org.apache.wicket.IRequestTarget;
import org.apache.wicket.PageParameters;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;
import org.wicketstuff.annotation.mount.MountPath;
import org.wicketstuff.annotation.strategy.MountHybrid;

import com.gr.staffpm.StaffPMWebPage;
import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.projects.service.ProjectService;
import com.gr.staffpm.tasks.service.TaskService;


/**
 * @author Graham Rhodes 26 Apr 2011 14:33:21
 */
@MountHybrid
@MountPath(path = "secure/tasks")
public class Tasks extends StaffPMWebPage {

    @SpringBean(name = "projectService")
    private ProjectService projectService;
    @SpringBean(name = "taskService")
    private TaskService taskService;

    public Tasks(final PageParameters parameters) {
        int projectId = parameters.getInt("id");
        final Project project = projectService.getProject(projectId);
        getRequestCycle().setRequestTarget(new IRequestTarget() {

            @Override
            public void detach(RequestCycle requestCycle) {
                // Nothing to do here.
            }

            @Override
            public void respond(RequestCycle requestCycle) {
                StringBuilder str = new StringBuilder();
                List<Task> tasks = taskService.getUnresolvedProjectTasks(project);
                str.append("<Tasks>");
                DateTime start = new DateTime(project.getCreated());
                str.append("<Task>");
                str.append("<Id>1</Id>");
                str.append("<Name>" + project.getProjectName() + "</Name>");
                str.append("<StartDate>" + start.withTime(0, 0, 0, 0).toString() + "</StartDate>");
                str.append("<EndDate>" + new DateTime().plusMonths(8) + "</EndDate>");
                str.append("<PercentDone>0</PercentDone>");
                str.append("<ParentId/>");
                str.append("<IsLeaf>false</IsLeaf>");
                str.append("<Responsible>" + project.getOwner().getFullName() + "</Responsible>");
                str.append("</Task>");
                for (Task task : tasks) {
                    start = new DateTime(task.getDueDate());
                    str.append("<Task>");
                    str.append("<Id>" + task.getTaskId() + "</Id>");
                    str.append("<Name>" + task.getName() + "</Name>");
                    str.append("<StartDate>" + start.withTime(0, 0, 0, 0).toString() + "</StartDate>");
                    str.append("<EndDate>" + start.withTime(start.hourOfDay().getMaximumValue(), 59, 59, 59).toString() + "</EndDate>");
                    str.append("<PercentDone>0</PercentDone>");
                    str.append("<ParentId>1</ParentId>");
                    str.append("<IsLeaf>true</IsLeaf>");
                    str.append("<Responsible>" + task.getAssignee().getFullName() + "</Responsible>");
                    str.append("</Task>");
                }
                str.append("</Tasks>");
                requestCycle.getResponse().write(str.toString());
            }

        });
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.StaffPMWebPage#getTitle()
     */
    @Override
    public String getTitle() {
        return "StaffPM - Tasks.xml";
    }

}
